<?php

namespace App\Http\Controllers\Api\V1\Wechat;

use App\Http\Controllers\ApiBaseController;
use App\Libs\Commons\MyLogger;
use App\Libs\Commons\MyMessage;
use App\Services\Wechat\MiniProgramService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AuthController extends ApiBaseController
{
	public function session(Request $request)
	{

		try {
			$param = $request->all();
			$validator = Validator::make($param, [
				'code' => 'required'
			]);

			if ($validator->fails()) {
				return $this->fail(MyMessage::ERROR, $validator->errors()->first());
			}

			$app = MiniProgramService::getInstance();
			$result = $app->auth->session($param['code']);
			MyLogger::info('登录凭证校验', $result);
			return $this->success($result);
		} catch (\Throwable $e) {
			return response()->json(['code' => $e->getCode(), 'msg' => $e->getMessage()]);
		}
	}
}
